package com.huluobo.module_city.ui.activity

import android.content.Intent
import android.os.Bundle
import android.view.View
import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.huluobo.appbase.config.ArouterConfig
import com.huluobo.appbase.config.ParametersConfig
import com.huluobo.appbase.config.ResultCodeConfig
import com.huluobo.appbase.ui.BaseActivity
import com.huluobo.appbase.utils.DialogLoadingUtil
import com.huluobo.module_city.bean.CityDataReqData
import com.huluobo.module_city.databinding.ActivityCityDataBinding
import com.huluobo.module_city.ui.adapter.CityDataAdapter
import com.huluobo.module_city.ui.view.SlideBar
import com.huluobo.module_city.viewmodel.CityDataViewModel

/**
 * Created by lc on 2022/12/28.
 * 城市数据Activity
 */
@Route(path = ArouterConfig.CITY_DATA)
class CityDataActivity : BaseActivity<ActivityCityDataBinding>() {
    override fun getViewBinding(): ActivityCityDataBinding =
        ActivityCityDataBinding.inflate(layoutInflater)

    private val cityDataViewModel by viewModels<CityDataViewModel>()

    val cityList = mutableListOf<CityDataReqData.CitysData>()

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        init()
    }

    private fun init() {
        initView()
        loadData()
    }

    private fun loadData() {
        DialogLoadingUtil.showLoading(this, "请稍后")
        cityDataViewModel.loadCityData().observe(this, {
            it?.let {
                if (it.error_code == 0) {
                    //请求成功
                    it.result?.let { list ->
                        for (i in list.indices) {
                            val data = list[i].citys
                            data?.let {
                                for (index in data.indices) {
                                    cityList.add(data[index])
                                }
                            }
                        }
                        cityList.sort()
                        val cityDataAdapter =
                            CityDataAdapter(cityList, callback = { position, data ->
                                //选择数据的回调
                                val intent = Intent()
                                intent.putExtra(ParametersConfig.CITY_ID, data.city_id)
                                intent.putExtra(ParametersConfig.CITY_NAME, data.city)
                                setResult(
                                    ResultCodeConfig.RESULT_CODE_SELECT_CITY_SUCCESS,
                                    intent
                                )
                                finish()
                            })
                        mViewBinding.rvData.adapter = cityDataAdapter
                        DialogLoadingUtil.cancel()
                    }
                }
            }
        })
    }

    private fun initView() {
        mViewBinding.toolBar.setOnClickListener { finish() }

        mViewBinding.slideBar.onSectionChangeListener(object : SlideBar.OnSectionChangeListener {
            override fun onSectionChange(firstLetter: String) {
                mViewBinding.tvIndex.visibility = View.VISIBLE
                mViewBinding.tvIndex.text = firstLetter
                firstLetter.let {
                    cityList.let {
                        it.forEachIndexed { position, data ->
                            if (firstLetter == data.firstLetter) {
                                mViewBinding.rvData.scrollToPosition(position)
                                return
                            }
                        }
                    }
                }
            }

            override fun onSlideFinish() {
                mViewBinding.tvIndex.visibility = View.GONE
            }

        })
        val layoutManager = LinearLayoutManager(this)
        mViewBinding.rvData.layoutManager = layoutManager
    }
}